﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QFlow.Data;
using QFlow.Core;

namespace QFlow.Windows.Forms
{
    public static class LoggingManager
    {
        private static QFlowDbContext _Context;

        public static QFlowDbContext Context
        {
            get
            {
                return _Context;
            }
            set
            {
                _Context = value;
            }
        }

        /// <summary>
        /// Switches the users.
        /// </summary>
        /// <param name="user">The user.</param>
        public static void SwitchUsers(User user)
        {
            Data.AccountingLog log = new Data.AccountingLog()
            {
                CashirCredit = Context.Accounts.GetCashirAccount().Amount,
                SafeCredit = Context.Accounts.GetSafeAccount().Amount,
                PurchaseCredit = Accounting.PurchaseCredit,
                PurchaseReturn = Accounting.PurchaseReturn,
                SalesCredit = Accounting.SalesCredit,
                SalesReturn = Accounting.SalesReturn,
                TimeStamp = DateTime.Now,
                User = Context.Users.GetCurrentUser(),
                AccountingEvent = Context.AccountingEvents.GetLogoutEvent()
            };

            Context.AccountingLogs.InsertOnSubmit(log);
            Context.SubmitChanges();

            LoginUser(user);
        }

        /// <summary>
        /// Logins the user.
        /// </summary>
        /// <param name="user">The user.</param>
        public static void LoginUser(User user)
        {
            Data.AccountingLog log = new Data.AccountingLog()
            {
                CashirCredit = Context.Accounts.GetCashirAccount().Amount,
                SafeCredit = Context.Accounts.GetSafeAccount().Amount,
                PurchaseCredit = Accounting.PurchaseCredit,
                PurchaseReturn = Accounting.PurchaseReturn,
                SalesCredit = Accounting.SalesCredit,
                SalesReturn = Accounting.SalesReturn,
                TimeStamp = DateTime.Now,
                User = user,
                AccountingEvent = Context.AccountingEvents.GetLoginEvent()
            };

            Context.AccountingLogs.InsertOnSubmit(log);
            Context.SubmitChanges();
        }

        /// <summary>
        /// Logouts the user.
        /// </summary>
        /// <param name="user">The user.</param>
        public static void LogoutUser(User user)
        {
            Data.AccountingLog log = new Data.AccountingLog()
            {
                CashirCredit = Context.Accounts.GetCashirAccount().Amount,
                SafeCredit = Context.Accounts.GetSafeAccount().Amount,
                PurchaseCredit = Accounting.PurchaseCredit,
                PurchaseReturn = Accounting.PurchaseReturn,
                SalesCredit = Accounting.SalesCredit,
                SalesReturn = Accounting.SalesReturn,
                TimeStamp = DateTime.Now,
                User = user,
                AccountingEvent = Context.AccountingEvents.GetLogoutEvent()
            };

            Context.AccountingLogs.InsertOnSubmit(log);
            Context.SubmitChanges();
        }
    }
}
